package com.archie.xunji.guanzhu.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.archie.xunji.R;
import com.archie.xunji.guanzhu.entitys.People;
import com.lidroid.xutils.BitmapUtils;
import com.meg7.widget.CircleImageView;

import java.util.List;

/**
 * Created by xqx on 2015/11/10.
 */
public class PeopleAdapter extends BaseAdapter{
    private List<People> list;
    private Context context;
    private BitmapUtils bitmapUtils;
    public PeopleAdapter(List<People> list, Context context) {
        this.list = list;
        this.context = context;
        bitmapUtils = new BitmapUtils(context);
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (list != null) {
            ret = list.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;

        if (convertView != null) {
            view = convertView;
        }else{
            view = LayoutInflater.from(context).inflate(R.layout.item_people_list,parent,false);
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.head = (CircleImageView) view.findViewById(R.id.img_head);
            holder.name = (TextView) view.findViewById(R.id.people_username);
            holder.people_list_bg = (RelativeLayout) view.findViewById(R.id.people_list_bg);
            view.setTag(holder);
        }
        holder.name.setText(list.get(position).getNickname());
        bitmapUtils.configDefaultLoadingImage(R.mipmap.ui_def_user_ic);
        bitmapUtils.display(holder.head, list.get(position).getHeadurl());
        if (position%2==1)
            holder.people_list_bg.setBackgroundColor(context.getResources().getColor(R.color.gray));
        else
            holder.people_list_bg.setBackgroundColor(Color.WHITE);
        
//        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
//        holder.head.setImageDrawable(new CircleImageView(bitmap));
        return view;
    }

    private class ViewHolder{
        private CircleImageView head;
        private TextView name;
        private RelativeLayout people_list_bg;
    }
}
